package com.lagou.service.impl;

import com.github.dozermapper.core.Mapper;
import com.lagou.config.MailProperties;
import com.lagou.pojo.MailDTO;
import com.lagou.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @ClassName MailServiceImpl
 * @Description
 * @Author playboy
 * @Date 2021/6/25 4:29 下午
 * @Version 1.0
 **/
@Service
public class MailServiceImpl implements MailService {
	private static Logger log = LoggerFactory.getLogger(MailServiceImpl.class);

	@Autowired
	private MailProperties mailProperties;

	@Autowired
	private JavaMailSender javaMailSender;

	@Autowired
	private Mapper mapper;

	@Override
	public void sendSimpleMailMessage(MailDTO mailDTO) {
		if (StringUtils.isEmpty(mailDTO.getFrom())) {
			mailDTO.setFrom(mailProperties.getFrom());
			//抄送给自己
			mailDTO.setCc(new String[]{mailProperties.getFrom()});
		}
		SimpleMailMessage simpleMailMessage = mapper.map(mailDTO, SimpleMailMessage.class);
		javaMailSender.send(simpleMailMessage);
	}
}
